package com.vfa.fishing.object;

import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import com.vfa.fishing.GameActivity;
import com.vfa.fishing.MainActivity;
import com.vfa.fishing.common.Constant;

import android.util.Log;

public class Fish extends AbstractObject {
    public boolean Visible;
    private float  speedX;
    private float  speedY;
    public int kind;// loai ca
    public int direction;
    public int sideAppear;
    public int iTimer;
    public int iTerrible;
    public int Weight;
    public int iTimeToResetDirect;
    public boolean isReset;
    // Constructor
    public Fish(int x,int y,TiledTextureRegion pTextureRegion,int sideApp,int kind){
        super(x,y,pTextureRegion,sideApp);
        this.kind = kind;
        sideAppear = sideApp;// huong xuat hien cua con ca
        if(sideAppear == Constant.LEFTSIDE){
            direction = Constant.RIGHT;
        }
        else if(sideAppear == Constant.RIGHTSIDE){
            direction = Constant.LEFT;
        }
        // set van toc di chuyen cua ca
        if(kind == 0){
            speedX = GameActivity.rand.nextInt(10)+1;
        }
        else if(kind == 1){
            speedX = GameActivity.rand.nextInt(10)+3;
            speedY = GameActivity.rand.nextInt(10)+3;
        }
        Weight = 30;
        iTerrible = GameActivity.rand.nextInt(2);// 0 neu la ca hung du,1 neu la ca hien
        iTimeToResetDirect = 0;
        isReset = false;
        this.kindOfObject = Constant.FISH;
    }
    public void update() {
        iTimer = GameActivity.TIMER;
        iTimeToResetDirect ++;
        Log.e("LocNNT","iTimeToResetDirect:   ======================="+  iTimeToResetDirect);
        if(iTimeToResetDirect%50 == 0){
        	iTimeToResetDirect = 0;
        	isReset = false;
        }
        float  X = 0;
        float  Y = 0;
        X = this.getX();
        Y = this.getY();
//        Log.e("LocNNT","toa do X:   ======================="+  X);
//        Log.e("LocNNT","toa do Y:   ======================="+  Y);

        if(kind == 0) {
              if(!this.checkCollision()) {
            	  if(iTimer%20 == 0) {
                      speedX = GameActivity.rand.nextInt(10)+1;
                      speedY = GameActivity.rand.nextInt(3)+1;
                  }
                  if(direction == Constant.RIGHT) {
                      X += speedX;
                      this.setPosition(X, Y);
                  }
                  else if(direction == Constant.LEFT) {
                      X -= speedX;
                      this.setPosition(X, Y);
                  }
                  else if(direction == Constant.RIGHT_UP) {
                      X += speedX;
                      Y -= speedY;
                      this.setPosition(X, Y);
                  }
                  else if(direction == Constant.RIGHT_DOWN) {
                      X += speedX;
                      Y += speedY;
                      this.setPosition(X, Y);
                  }
                  else if(direction == Constant.LEFT_UP) {
                      X -= speedX;
                      Y -= speedY;
                      this.setPosition(X, Y);
                  }
                  else if(direction == Constant.LEFT_DOWN) {
                      X -= speedX;
                      Y += speedY;
                      this.setPosition(X, Y);
                  }
              }
        }
        else if(kind == 1) {
        	if(!this.checkCollision()) {
          	  if(iTimer%20 == 0) {
                    speedX = GameActivity.rand.nextInt(10)+1;
                    speedY = GameActivity.rand.nextInt(3)+1;
                }
                if(direction == Constant.RIGHT) {
                    X += speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT) {
                    X -= speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_UP) {
                    X += speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_DOWN) {
                    X += speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_UP) {
                    X -= speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_DOWN) {
                    X -= speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
            }
        }
        else if(kind == 2) {
        	if(!this.checkCollision()) {
          	  if(iTimer%20 == 0) {
                    speedX = GameActivity.rand.nextInt(10)+1;
                    speedY = GameActivity.rand.nextInt(3)+1;
                }
                if(direction == Constant.RIGHT) {
                    X += speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT) {
                    X -= speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_UP) {
                    X += speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_DOWN) {
                    X += speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_UP) {
                    X -= speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_DOWN) {
                    X -= speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
            }
        }
        else if(kind == 3) {
        	if(!this.checkCollision()) {
          	  if(iTimer%20 == 0) {
                    speedX = GameActivity.rand.nextInt(10)+1;
                    speedY = GameActivity.rand.nextInt(3)+4;
                }
                if(direction == Constant.RIGHT) {
                    X += speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT) {
                    X -= speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_UP) {
                    X += speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_DOWN) {
                    X += speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_UP) {
                    X -= speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_DOWN) {
                    X -= speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
            }
        }
        else if(kind == 4) {
        	if(!this.checkCollision()) {
          	  if(iTimer%20 == 0) {
                    speedX = GameActivity.rand.nextInt(10)+1;
                    speedY = GameActivity.rand.nextInt(3)+1;
                }
                if(direction == Constant.RIGHT) {
                    X += speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT) {
                    X -= speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_UP) {
                    X += speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_DOWN) {
                    X += speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_UP) {
                    X -= speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_DOWN) {
                    X -= speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
            }
        }
        else if(kind == 5) {
        	if(!this.checkCollision()) {
          	  if(iTimer%20 == 0) {
                    speedX = GameActivity.rand.nextInt(10)+1;
                    speedY = GameActivity.rand.nextInt(3)+1;
                }
                if(direction == Constant.RIGHT) {
                    X += speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT) {
                    X -= speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_UP) {
                    X += speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_DOWN) {
                    X += speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_UP) {
                    X -= speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_DOWN) {
                    X -= speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
            }
        }
        else if(kind == 6) {
        	if(!this.checkCollision()) {
          	  if(iTimer%20 == 0) {
                    speedX = GameActivity.rand.nextInt(10)+1;
                    speedY = GameActivity.rand.nextInt(3)+1;
                }
                if(direction == Constant.RIGHT) {
                    X += speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT) {
                    X -= speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_UP) {
                    X += speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_DOWN) {
                    X += speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_UP) {
                    X -= speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_DOWN) {
                    X -= speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
            }
        }
        else if(kind == 7) {
        	if(!this.checkCollision()) {
          	  if(iTimer%20 == 0) {
                    speedX = GameActivity.rand.nextInt(10)+1;
                    speedY = GameActivity.rand.nextInt(3)+1;
                }
                if(direction == Constant.RIGHT) {
                    X += speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT) {
                    X -= speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_UP) {
                    X += speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_DOWN) {
                    X += speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_UP) {
                    X -= speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_DOWN) {
                    X -= speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
            }
        }
        else if(kind == 8) {
        	if(!this.checkCollision()) {
          	  if(iTimer%20 == 0) {
                    speedX = GameActivity.rand.nextInt(10)+1;
                    speedY = GameActivity.rand.nextInt(3)+1;
                }
                if(direction == Constant.RIGHT) {
                    X += speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT) {
                    X -= speedX;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_UP) {
                    X += speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.RIGHT_DOWN) {
                    X += speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_UP) {
                    X -= speedX;
                    Y -= speedY;
                    this.setPosition(X, Y);
                }
                else if(direction == Constant.LEFT_DOWN) {
                    X -= speedX;
                    Y += speedY;
                    this.setPosition(X, Y);
                }
            }
        }
    }
    @Override
    public void Eat() {
        // TODO Auto-generated method stub

    }
	@Override
	public void resetDirection() {
		// TODO Auto-generated method stub
		if(isReset)
			return;
		if(direction == Constant.RIGHT){
			Log.e("LocNNT", "huong ban dau la  "+"==========================================="+direction);
			int randdomDirect = GameActivity.rand.nextInt(5);
			direction = Constant.arrDirect1[randdomDirect];
			isReset = true;
			Log.e("LocNNT", "huong sau la  "+"==========================================="+direction);
			return;
		}
		else if(direction == Constant.LEFT){
			int randdomDirect = GameActivity.rand.nextInt(5);
			direction = Constant.arrDirect2[randdomDirect];
			isReset = true;
			return;
		}
		else if(direction == Constant.RIGHT_UP){
			int randdomDirect = GameActivity.rand.nextInt(5);
			direction = Constant.arrDirect3[randdomDirect];
			isReset = true;
			return;
		}
		else if(direction == Constant.RIGHT_DOWN){
			int randdomDirect = GameActivity.rand.nextInt(5);
			direction = Constant.arrDirect4[randdomDirect];
			isReset = true;
			return;
		}
		else if(direction == Constant.LEFT_UP){
			int randdomDirect = GameActivity.rand.nextInt(5);
			direction = Constant.arrDirect5[randdomDirect];
			isReset = true;
			return;
		}
		else if(direction == Constant.LEFT_DOWN){
			int randdomDirect = GameActivity.rand.nextInt(5);
			direction = Constant.arrDirect6[randdomDirect];
			isReset = true;
			return;
		}	
		
	}
}